<?php
namespace Admin\Controller;

use Base\Controller\AdminBaseController;

class RoleController extends AdminBaseController {

    protected $roleModel;
    protected $menuModel;
    protected $roleMenuModel;
    protected $userRoleModel;

    public function _initialize() {
        parent::_initialize();

        $this->roleModel = D("Admin/Role");
        $this->menuModel = D("Admin/Menu");
        $this->roleMenuModel = D("Admin/RoleMenu");
        $this->userRoleModel = D("Admin/UserRole");
    }
	
	/******************************** JSON *********************************/
	/** 添加信息 */
	public function save() {
		$roleJson = file_get_contents('php://input');
		$role = json_decode($roleJson, true);
		
		// setting
		$role['status'] = 1;

		// 执行添加
		$result = $this->roleModel->add($role);
		if (!$result) {
			$data['code'] = 499;
			$data['msg'] = '添加失败！';
			$this->ajaxReturn($data);
		}

		$newRole = $this->roleModel->where(array("id" => $result))->find();
			
		$data['code'] = 0;
		$data['msg'] = '添加成功！';
		$data['obj'] = $newRole;
		$this->ajaxReturn($data);
	}
	
	/** 查看所有  */
    public function getAll() {
		$result = $this->roleModel->select();
// 		dump($result);
		
	    // return
	    $this->ajaxReturn($result);
	}

	/** 查看所有列表 */
	public function dataList() {
		$dataList = $this->roleModel->dataList($_GET);
	    $this->ajaxReturn($dataList);
	}
	 
	/** 查看某信息 */
	public function find() {
		$id = $_GET['id'];
		
		$role = $this->roleModel->where(array("id" => $id))->find();
		if (!$role) {
			$data['code'] = 499;
			$data['msg'] = '未找到目标！';
			$this->ajaxReturn($data);
		}
		
	    $data['code'] = 0;
	    $data['msg'] = 'OK';
	    $data['obj'] = $role;
	    $this->ajaxReturn($data);
	}

	/** 修改某信息 */
	public function update() {
		$id = $_GET['id'];
		$roleJson = file_get_contents('php://input');
		$role = json_decode($roleJson, true);
		
		// 执行更新
		$result = $this->roleModel->where(array("id" => $id))->save($role);
		if (false === $result) {
			$data['code'] = 499;
			$data['msg'] = '编辑失败！';
			$this->ajaxReturn($data);
		}

		$updatedModel = $this->roleModel->where(array("id" => $id))->find();
			
		$data['code'] = 0;
		$data['msg'] = '编辑成功！';
		$data['obj'] = $updatedModel;
		$this->ajaxReturn($data);
	}
	
	/** 删除某信息 */
	public function delete() {
		$id = $_GET['id'];
		
		$role = $this->roleModel->where(array("id" => $id))->find();
		if (!$role) {
			$data['code'] = 499;
			$data['msg'] = '未找到目标！';
			$this->ajaxReturn($data);
		}

		if (!$this->roleModel->delete($id)) {
		    $data['code'] = 499;
		    $data['msg'] = '删除失败！';
		    $this->ajaxReturn($data);
		}
			
		$data['code'] = 0;
		$data['msg'] = '删除成功！';
		$this->ajaxReturn($data);
	}

	/** 为某分配资源 */
	public function addMenus() {
		$id = $_GET['id'];
		$resIdListJson = file_get_contents('php://input');
		$resIdList = json_decode($resIdListJson, true);
		
		// remove all menus
		$this->roleMenuModel->where(array("role_id" => $id))->delete();
		
		// add new menus
		foreach ($resIdList as $v) {
			$this->roleMenuModel->add(array("role_id" => $id, "menu_id" => $v));
		}

		$updatedRole = $this->roleModel->where(array("id" => $id))->find();
		
		$data['code'] = 0;
		$data['msg'] = '分配资源成功！';
		$data['obj'] = $updatedRole;
		$this->ajaxReturn($data);
	}
	
	/** getRoleId4User 分配menu选择用  */
    public function getRoleIds4User() {
		$userId = $_GET['userId'];
		
		$roleIds = $this->userRoleModel->where(array("user_id" => $userId))->getField('role_id', true);

		$data['code']  = 0;
    	$data['msg'] = 'ok';
    	$data['obj'] = $roleIds;
    	$this->ajaxReturn($data);
	}

	/******************************** HTML *********************************/
	/** index */
    public function index() {
		$this->display(':role');
		return;
	}

}

